package net.huashitong.Indicators.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.scheduling.web.RevenueController;

import com.sirdc.modules.core.web.bind.SpringContextHolder;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;

public class DataUtils {
	private static IndicatorBreakdownService indicatorBreakdownService = SpringContextHolder.getBean(IndicatorBreakdownService.class);
	/**
	 * 数据转换
	 * @param databean
	 * @param breakdown
	 * @return
	 */
	public static IndicatorBreakdownDatabean dataConversion(IndicatorBreakdownDatabean databean,IndicatorBreakdown breakdown){
		if(StringUtils.isNotBlank(breakdown.getSeasonsComparison())){
			databean.setSeasonsComparison(breakdown.getSeasonsComparison());
		}
		databean.setDate(breakdown.getDate());
		databean.setCurrentMonth(breakdown.getCurrentMonth());
		return databean;
	}
	/**
	 * 数据处理当季可比价格
	 * @param breakdowns
	 * @param breakdownDatabeans
	 * @return
	 */
	public static List<IndicatorBreakdownDatabean> detailData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
		DecimalFormat df=new DecimalFormat("##.##");
		for(int i = 0 ;i<breakdowns.size();i++){
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			IndicatorBreakdown breakdown = breakdowns.get(i);
			if(i == breakdowns.size()-1){
				breakdown.setSeasonsComparison(breakdown.getCumulative());
			}else {
				IndicatorBreakdown breakdown2 = breakdowns.get(i+1);
				Double double1 = Double.parseDouble(breakdown.getCumulative());
				Double double2 = Double.parseDouble(breakdown2.getCumulative());
				breakdown.setSeasonsComparison(df.format(double1-double2));
			}
			databean = dataConversion(databean, breakdown);
			breakdownDatabeans.add(databean);
		}
		return breakdownDatabeans;
	}
	
	/**
	 * 处理可比累计价格
	 * @param breakdowns
	 * @param breakdownDatabeans
	 * @return
	 */
	public static List<IndicatorBreakdownDatabean> detailcumulativeData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
		DecimalFormat df=new DecimalFormat("#.##");
		for(int i = 0 ;i<breakdowns.size();i++){
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			IndicatorBreakdown breakdown = breakdowns.get(i);
			if(StringUtils.isNotBlank(breakdown.getCumulative())){
				databean.setCumulative(df.format(Math.abs(Double.parseDouble(breakdown.getCumulative()))));
			}
			if(StringUtils.isNotBlank(breakdown.getRatio())){
				databean.setRatio(df.format(Double.parseDouble(breakdown.getRatio())));
			}
			databean = dataConversion(databean, breakdown);
			breakdownDatabeans.add(databean);
		}
		return breakdownDatabeans;
	}
	/**
	 * 当月预警比率
	 * @param filter
	 * @return
	 */
	public static Double warning(IndicatorBreakdownFilter filter){
		Double double1 = Double.parseDouble(filter.getCurrentMonth().substring(4));
		double1 = double1/12*100;
		double1=25.0;
		return double1;
	}
	
	public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
	public static final String[] quas = {"12","09","06","03"};
	public static final String[] years={"2022", "2021", "2020","2019","2018","2017","2016","2015"};
	/**
	 * 判断月份或者季度
	 * @param filter
	 * @return
	 */
	public static String getValue(IndicatorBreakdownFilter filter){
		if(IndustryNameUtils.SolidIndexId.equals(filter.getIndustryId())||IndustryNameUtils.ZeorIndexId.equals(filter.getIndustryId())|| IndustryNameUtils.SizeOfIndexId.equals(filter.getIndustryId())||IndustryNameUtils.IndustryIndexId.equals(filter.getIndustryId())
				||IndustryNameUtils.PublicTotalIndexId.equals(filter.getIndustryId())||IndustryNameUtils.PublicIndexId.equals(filter.getIndustryId())
				||IndustryNameUtils.RealOutForeignIndexId.equals(filter.getIndustryId())||IndustryNameUtils.OutTotalIndexId.equals(filter.getIndustryId())){
			return "0";
		}
		if(IndustryNameUtils.GDPIndexId.equals(filter.getIndustryId())||IndustryNameUtils.ProOneIndexId.equals(filter.getIndustryId())
				||IndustryNameUtils.ProTwoIndexId.equals(filter.getIndustryId())||IndustryNameUtils.ProThitIndexId.equals(filter.getIndustryId())
				||IndustryNameUtils.BuildIndexId.equals(filter.getIndustryId())||IndustryNameUtils.CityIncomeIndexId.equals(filter.getIndustryId())
				||IndustryNameUtils.CountryIncomeIndexId.equals(filter.getIndustryId())){
			return "1";
		}
		return "0";
	}
	
	/**
	 * 
	 * @param judge
	 * @param filter 
	 * @return
	 */
	public static String getValueMonth(String judge, IndicatorBreakdownFilter filter) {
		if("0".equals(judge)){
			for(int j=0;j<DataUtils.years.length;j++){
			   for(int i=0; i<DataUtils.months.length;i++){
				filter.setCurrentMonth(years[j]+DataUtils.months[i]);
				List<IndicatorBreakdown> breakdown = indicatorBreakdownService.queryMonthValueSec(filter);
				if(ObjectUtils.isNotBlank(breakdown)){
					return years[j]+DataUtils.months[i];
				  }
				
			}
			
		}	
		}else if("1".equals(judge)){
			for(int j=0;j<DataUtils.years.length;j++){
			   for(int i=0; i<DataUtils.quas.length;i++){
			   	 filter.setDate(years[j]+DataUtils.quas[i]);
				 filter.setCurrentMonth(years[j]+DataUtils.quas[i]);
				 System.out.print("***********************="+years[j]+DataUtils.quas[i]);
				List<IndicatorBreakdown> breakdown = indicatorBreakdownService.queryMonthValueSec(filter);
				   ;if(ObjectUtils.isNotBlank(breakdown)){
					System.out.print("@@@@@@@@@@@@@@@@@@="+years[j]+DataUtils.quas[i]);
					 return years[j]+DataUtils.quas[i];
				}
			
			}
		  }
		}
		return null;
	}

	/**
	 * 自定义保留几位小数
	 * @param val
	 * @param t
	 * @return
	 */
	public static String formaterDecimal(String val,int t ){
		BigDecimal bg  = new BigDecimal(Double.parseDouble(val));
		Double f1 = bg.setScale(t,BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1.toString();
	}

	public static List<String > getMonth() {
		List<String> sList = Arrays.asList("02","03","04","05","06","07","08","09","10","11","12");
		List<String> list   = new ArrayList<>();
		list.addAll(sList);
		return list;
	}


}
